package com.bcmusic.authorization.service.impl;

import com.bcmusic.authorization.dao.AuthorizationMapper;
import com.bcmusic.authorization.pojo.Authorization;
import com.bcmusic.authorization.service.AuthorizationService;
import com.bcmusic.order.feign.OrderCartFeign;
import com.bcmusic.order.pojo.OrderCart;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import entity.SmartBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.test.annotation.Rollback;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/****
 * @Author:admin
 * @Description:Authorization业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class AuthorizationServiceImpl implements AuthorizationService {


    @Autowired
    private AuthorizationMapper authorizationMapper;

    @Autowired
    private OrderCartFeign orderCartFeign;

    /**
     * Authorization构建查询对象
     * @param authorization
     * @return
     */
    public Example createExample(Authorization authorization){
        Example example=new Example(Authorization.class);
        Example.Criteria criteria = example.createCriteria();
        if(authorization!=null){
            // 授权id
            if(!StringUtils.isEmpty(authorization.getAuthorizationId())){
                criteria.andEqualTo("authorizationId",authorization.getAuthorizationId());
            }
            // 音乐id
            if(!StringUtils.isEmpty(authorization.getMusicId())){
                criteria.andEqualTo("musicId",authorization.getMusicId());
            }
            // 版权注册码
            if(!StringUtils.isEmpty(authorization.getAuthorizationCode())){
                criteria.andEqualTo("authorizationCode",authorization.getAuthorizationCode());
            }
            // 授权类型
            if(!StringUtils.isEmpty(authorization.getAllowType())){
                criteria.andEqualTo("allowType",authorization.getAllowType());
            }
            // 授权地域
            if(!StringUtils.isEmpty(authorization.getAllowRegion())){
                criteria.andEqualTo("allowRegion",authorization.getAllowRegion());
            }
            // 开始时间
            if(!StringUtils.isEmpty(authorization.getAuthorizationStartTime())){
                criteria.andEqualTo("authorizationStartTime",authorization.getAuthorizationStartTime());
            }
            // 截止时间
            if(!StringUtils.isEmpty(authorization.getAuthorizationEndTime())){
                criteria.andEqualTo("authorizationEndTime",authorization.getAuthorizationEndTime());
            }
            // 被授权方名字
            if(!StringUtils.isEmpty(authorization.getOwnerName())){
                criteria.andEqualTo("ownerName",authorization.getOwnerName());
            }
            // 被授权方id
            if(!StringUtils.isEmpty(authorization.getOwnerNumber())){
                criteria.andEqualTo("ownerNumber",authorization.getOwnerNumber());
            }
            // 0表示过期了，一表示正在生效，2表示还没有生效
            if(!StringUtils.isEmpty(authorization.getValidStatus())){
                criteria.andEqualTo("validStatus",authorization.getValidStatus());
            }
            //
            if(!StringUtils.isEmpty(authorization.getCreateTime())){
                criteria.andEqualTo("createTime",authorization.getCreateTime());
            }
            //
            if(!StringUtils.isEmpty(authorization.getModificationTime())){
                criteria.andEqualTo("modificationTime",authorization.getModificationTime());
            }
        }
        return example;
    }

    @Rollback
    @Override
    public Integer addAuthorization(Integer orderId) {
        List<OrderCart> data = orderCartFeign.getOrderCartListByOrderId(orderId).getData();
        if(data==null){
            return 0;
        }
        List<Authorization> authorizations = SmartBeanUtil.copyList(data, Authorization.class);
        for (int i = 0; i < authorizations.size(); i++) {
            authorizations.get(i).setOwnerNumber(data.get(i).getUserNumber());
            authorizations.get(i).setValidStatus(1);
            authorizations.get(i).setCreateTime(new Date());
            authorizations.get(i).setModificationTime(new Date());
            //随便加密一个先
            authorizations.get(i).setAuthorizationCode(new BCryptPasswordEncoder().encode(authorizations.get(i).getMusicId()+authorizations.get(i).getOwnerNumber()));
        }
        //版权模块还是校验一下数据库吧
        try {
            int i = authorizationMapper.insertList(authorizations);
        }catch (Exception e){
            return 0;
        }
        return 1;
    }
}
